﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.Text.RegularExpressions;

namespace mamnontuoitho7.module
{
    public partial class ParentWarning : System.Web.UI.Page
    {
        private Notice _notice = new Notice();
        private Member _member = new Member();
        protected General _general = new General();
        protected Parent _parent = new Parent();

        protected DataTable _dt, _dtClass;
        protected int _pageflag;
        protected int _currentbar = 1;

        protected int _totalPages;
        protected int _totalNoticeNumber;
        protected int _totalBars;

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện
        protected void Page_Load(object sender, EventArgs e)
        {

            //Check Permission
            if (Session["memberID"].ToString() != "" && Session["role"].ToString() == "4")
            {
                try
                {
                    if (_parent.CheckParentAvailability(Session["memberID"].ToString()) == "2")
                    {
                        _dtClass = _member.GetClassByParent(Session["memberID"].ToString());
                        string strClassID = "", strtemp = "";
                        int a = _dtClass.Rows.Count;


                        for (int i = 0; i < a; i++)
                        {
                            if (i == a - 1)
                            {
                                strtemp = _dtClass.Rows[i]["ClassID"].ToString();
                            }
                            else
                            {
                                strtemp = _dtClass.Rows[i]["ClassID"].ToString() + ",";
                            }
                            strClassID += strtemp;
                        }

                        ////////////////////start paging
                        _totalNoticeNumber = _notice.TotalNoticeOfOneClass(strClassID).GetValueOrDefault();
                        _totalPages = _general.Divide(_totalNoticeNumber, ConstantClass._pageStepConversation);
                        _totalBars = _general.Divide(_totalPages, ConstantClass._barStepConversation);

                        if (Request.QueryString["page"] != null)// khi bấm vào các ô số
                        {
                            string temp = Request.QueryString["page"].ToString();
                            Match match = Regex.Match(temp, @"([0-9])$", RegexOptions.IgnoreCase);

                            if (temp.Length <= 4 && match.Success)
                            {
                                _pageflag = Convert.ToInt16(temp);// trang hiện hành
                                if (_pageflag <= _totalPages)
                                {
                                    _currentbar = _general.Divide(_pageflag, ConstantClass._barStepConversation);
                                    Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                                }
                                else
                                {
                                    _pageflag = 1;
                                    _currentbar = 1;
                                    Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                                }
                            }
                        }
                        if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
                        {
                            _currentbar = 1;
                            _pageflag = 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }

                        if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
                        {
                            _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                            if (_currentbar < _totalBars)
                            {
                                _currentbar += 1;
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                            _pageflag = (_currentbar - 1) * ConstantClass._barStepConversation + 1;

                        }

                        if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
                        {
                            _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                            if (_currentbar > 1)
                            {
                                _currentbar -= 1;
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                            _pageflag = (_currentbar - 1) * ConstantClass._barStepConversation + 1;
                        }

                        _dt = _notice.GetNoticePaged(_pageflag, ConstantClass._pageStepConversation, strClassID);
                        _beginPage = (_currentbar - 1) * ConstantClass._barStepConversation + 1;
                        _endPage = _currentbar * ConstantClass._barStepConversation;

                        if (_endPage > _totalPages)
                        {
                            _endPage = _totalPages;
                        }
                        //////////////////////////////End Paging
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorLoginPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }

            Session["Common_articleTypeID"] = "1";

        }
    }
}